package com.boot41.controller;

import com.boot41.entity.User;
import com.boot41.mapper.UserMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wxd
 * @version 1.0
 */
@RestController
public class UserController {
    final
    UserMapper mapper;

    public UserController(UserMapper mapper) {
        this.mapper = mapper;
    }

    @RequestMapping("/reg")
    public int reg(@RequestBody User user) {
        User u = mapper.selectByUsername(user.getUsername());
        if (u == null) {
            if ("".equals(user.getUsername()) || "".equals(user.getPassword())) {
                return 3;
            }
            mapper.insert(user);
            return 1;
        }
        return 2;
    }

    @RequestMapping("selectUser")
    public List<User> userList() {
        return mapper.select();
    }

    @RequestMapping("/deleteUser")
    public void delete(int id) {
        mapper.deleteById(id);
    }

    @RequestMapping("selectUserById")
    public User selectUserById(int id) {
        return mapper.selectUserById(id);
    }

    @RequestMapping("updateUserById")
    public void updateUserById(@RequestBody User user) {
        mapper.updateUserById(user);
    }
}
